/<template>
  <div>
      <h1>注册页面</h1>
      <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="用户名"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="密码"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import axios from "../config/axios.js";
import { Toast } from 'vant';
export default {
    data(){
        return{
            username:'',
            password:'',
            timer:null
        }
    },
    methods:{
        onSubmit(){
          axios.post('/toReg',{
            username:this.username,
            password:this.password
          }).then(res=>{
            if(res.data.code==402){
              Toast('该用户已注册，直接登录')
              this.$router.push('/login')
            }
            if(res.data.code==201){
              Toast('注册成功后自动跳转到登录页面')
              this.timer=setTimeout(()=>{
                this.$router.push('/login')
              },2000)
            }
          })
        }
    },
  beforeDestroy() {
    clearTimeout(this.timer)
  },
}
</script>

<style>

</style>